import 'package:flutter/material.dart';
import '../constants/app_colors.dart';
import '../models/diary.dart';
import '../utils/date_util.dart';

class DiaryCard extends StatelessWidget {
  final Diary diary;
  final VoidCallback? onTap;

  const DiaryCard({
    Key? key,
    required this.diary,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final date = DateUtil.parseDate(diary.date);
    final dateStr = DateUtil.formatDateFriendly(date);
    
    return Card(
      margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
      ),
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(16),
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    dateStr,
                    style: Theme.of(context).textTheme.titleMedium?.copyWith(
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Text(
                    diary.createdAt.substring(11, 16), // 只显示时:分
                    style: Theme.of(context).textTheme.bodySmall,
                  ),
                ],
              ),
              const Divider(height: 16),
              Text(
                diary.content,
                maxLines: 3,
                overflow: TextOverflow.ellipsis,
                style: Theme.of(context).textTheme.bodyMedium,
              ),
              if (diary.tags != null && diary.tags!.isNotEmpty) ...[
                const SizedBox(height: 12),
                Wrap(
                  spacing: 8,
                  runSpacing: 8,
                  children: diary.tags!.map((tag) {
                    return _buildTagChip(tag, context);
                  }).toList(),
                ),
              ],
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTagChip(MoodTag tag, BuildContext context) {
    final color = AppColors.getTagCategoryColor(tag.category);
    
    return Chip(
      label: Text(
        tag.name,
        style: TextStyle(
          fontSize: 12,
          color: color,
        ),
      ),
      backgroundColor: color.withOpacity(0.1),
      padding: EdgeInsets.zero,
      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
      visualDensity: VisualDensity.compact,
    );
  }
} 